package com.flyan.danmuhunter.bilibili.api;

import com.alibaba.fastjson.JSON;
import com.flyan.danmuhunter.bilibili.api.common.CommonRespResult;
import com.flyan.danmuhunter.common.utils.HttpUtil;

import java.io.IOException;

import static com.flyan.danmuhunter.common.utils.Helper._not_null;
import static com.flyan.danmuhunter.common.utils.Helper._str_not_black;

/**
 * @author flyan
 * @version 1.0
 * @date 2022/6/8
 */
public class Apis {

    public static final int SUCCESS_CODE = 0;

    /**
     * 通过直播间号获取真正的房间号 roomid 接口
     */
    private static final String REAL_ROOMID_API = "https://api.live.bilibili.com/room/v1/Room/room_init?id=$id";

    public static Long readRoomid(long id) throws IOException {
        String resp = HttpUtil.get(REAL_ROOMID_API.replace("$id", String.valueOf(id)));
        if(_str_not_black(resp)) {
            CommonRespResult result = JSON.parseObject(resp, CommonRespResult.class);
            if(callIsSuccess(result)) {
                return result.getData().getLong("room_id");
            }
        }

        return null;
    }

    private static boolean callIsSuccess(CommonRespResult result) {
        return _not_null(result) && (result.getCode() == SUCCESS_CODE);
    }

}
